﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using AjaxPro;

public partial class tset4 : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.TextBox tbSearch;
    public class PackAge
    {
        public Guid Id
        {
            get;
            set;
        }

        public String Name
        {
            get;
            set;
        }

        public Int32 Num
        {
            get;
            set;
        }

        public String Note
        {
            get;
            set;
        }

    }

    private List<PackAge> lists;
    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.RegisterTypeForAjax(typeof(tset4));
        if (HttpContext.Current.Session["lists"] != null)
        {
            lists = HttpContext.Current.Session["lists"] as List<PackAge>;
        }
        else
        {
            lists = new List<PackAge>();
        }

        if (HttpContext.Current.Request["action"] == "del")
        {
            DelPackAge(HttpContext.Current.Request["id"]);
        }

        if (!IsPostBack)
        {
            if (Session["userid"] == null)
            {
                Response.Write("<script>alert('请先登录')</script>");
                Response.Write("<script type=\"text/javascript\">top.location.href=\"login.aspx\";</script>");
            }
            else
            {
                binddata();
                txt3.Text = (DateTime.Now).ToString();
            }
        }
    }

    [AjaxMethod()]
    public ArrayList GetSearchItems(string query)
    {
        ArrayList items = new ArrayList();

        string myConnectionString =
            "server=HOME-D340431992\\SQLEXPRESS;database=medical; uid=sa;pwd=123";
        string mySelectQuery =
            string.Format(
            "select name from PackageName where CHARINDEX('{0}',LOWER(input_code)) = 1",
            query);
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
        myConnection.Open();
        SqlDataReader myReader =
            myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        while (myReader.Read())
        {
            items.Add(myReader.GetString(0));
        }
        myReader.Close();

        return items;
    }


    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {

    }
    #endregion

    private void DelPackAge(string p)
    {
        PackAge package = lists.SingleOrDefault(t => t.Id == new Guid(p));
        if (package != null)
        {
            lists.Remove(package);
            Session["lists"] = lists;
            this.rtpPageAge.DataSource = lists;
            this.rtpPageAge.DataBind();
        }
    }

    protected void bton_Click(object sender, EventArgs e)
    {
        if (txt3.Text == "")
        {
            Response.Write("<script>alert('请先填写好申请单')</script>");
        }
        else
        {
            int i;
            string id = txt3.Text;
            string strins = "INSERT INTO applylist (id, opeartor_id, appllytime, note, lend_id, status) VALUES ('" + id + "', '" + Session["userid"].ToString() + "', '" + (DateTime.Now).ToString() + "', 'null', 'null', '');";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["medical"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand(strins, con);
            cmd.ExecuteNonQuery();
            for (i = 0; i < lists.Count; i++)
            {
                string strinsp = "INSERT INTO applylistpack (list_id,packname_id,number) values ('" + id + "','" + lists[i].Name + "','" + lists[i].Num.ToString() + "');";
                SqlCommand cmd1 = new SqlCommand(strinsp, con);
                cmd1.ExecuteNonQuery();
            }
            con.Close();
            btoff_Click(sender, e);
            Response.Write("<script>alert('申请单" + id + "提交成功')</script>");
        }
    }

    protected void binddata()
    {
        string str_id = Session["userid"].ToString();
        sqldata sd = new sqldata();
        string str = "SELECT package_id FROM usedpackage,opeartorman where usedpackage.unit_id=opeartorman.unit_id and opeartorman.no='" + str_id + "';";
        sd.dropdownlistdatabind(str, DDL2);
        ListItem nullList = new ListItem("", null);
        DDL2.Items.Insert(0, nullList);
    }

    protected void btadd_Click(object sender, EventArgs e)
    {
        int flag=0;
        if ((search.Value != "" || DDL2.Text!="") && txt1.Text != "")
        {
            for (int i = 0; i < lists.Count; i++)
            {
                if (lists[i].Name == search.Value || lists[i].Name == DDL2.Text)
                {
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                if (search.Value != "")
                {
                    lists.Add(new PackAge()
                    {
                        Id = Guid.NewGuid(),
                        Name = search.Value,
                        Num = Convert.ToInt32(txt1.Text),
                        Note = txt2.Text
                    });
                }
                else
                {
                    lists.Add(new PackAge()
                    {
                        Id = Guid.NewGuid(),
                        Name = DDL2.Text,
                        Num = Convert.ToInt32(txt1.Text),
                        Note = txt2.Text
                    });
                }
                HttpContext.Current.Session["lists"] = lists;
                this.rtpPageAge.DataSource = lists;
                this.rtpPageAge.DataBind();
            }
            else
            {
                Response.Write("<script>alert('包名重复')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('包名和数量不能为空')</script>");
        }
    }
    protected void btoff_Click(object sender, EventArgs e)
    {
        txt1.Text = "";
        txt2.Text = "";
        txt3.Text = "";
        lists.Clear();
        HttpContext.Current.Session["lists"] = lists;
        this.rtpPageAge.DataSource = lists;
        this.rtpPageAge.DataBind();
    }
    protected void btset_Click(object sender, EventArgs e)
    {
        Response.Write("<script type=\"text/javascript\">window.open('pageset.aspx','_blank');</script>");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script type=\"text/javascript\">window.open('newslist.aspx','_blank');</script>"); 
    }
    protected void rtpPageAge_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void txt2_TextChanged(object sender, EventArgs e)
    {

    }
}
